package com.mohe.zhihui.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.zhihui.dao.IDao;
import com.mohe.zhihui.model.Activity;
import com.mohe.zhihui.model.Project;
import com.mohe.zhihui.service.interfaces.IActivity;
import com.mohe.zhihui.utils.Page;

public class ActivityImpl implements IActivity {

	private IDao<Activity> activityDao;
	
	public IDao<Activity> getActivityDao() {
		return activityDao;
	}

	public void setActivityDao(IDao<Activity> activityDao) {
		this.activityDao = activityDao;
	}

	@Override
	public List<Activity> getActivityList(Activity activity, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from Activity where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StringUtils.isBlank(activity.getTitle())){
			where.append(" and title like ?");
			params.add("%%"+activity.getTitle()+"%%");
		}
		where.append(" order by createDate desc");
		List<Activity> list = activityDao.query(where.toString(), (page.getCurrPage()-1)*page.getPageSize(), page.getPageSize(), params);
		if(list!=null && list.size()>0){
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(Activity activity) {
		StringBuffer where = new StringBuffer();
		where.append("from Activity where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StringUtils.isBlank(activity.getTitle())){
			where.append(" and title like ?");
			params.add("%%" + activity.getTitle() + "%%");
		}
		return activityDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveActivity(Activity activity) {
		return activityDao.insert(activity);
	}

	@Override
	public Activity getActivityById(Long timeKey) {
		Activity activity = activityDao.getByKey(timeKey);
		if(activity != null){
			return activity;
		}
		return null;
	}

	@Override
	public boolean updateActivity(Activity activity) {
		return activityDao.update(activity);
	}

	@Override
	public boolean delActivity(Activity activity) {
		return activityDao.delete(activity.getTimeKey());
	}

	@Override
	public List<Activity> getAllActivity() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return activityDao.query(where.toString(), null);
	}

}
